/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {boolean}
 */
 var isBalanced = function(root) {
    const getDeep = (root)=>{
        if(!root) return 0
        let left = getDeep(root.left)
        if(left==-1) return -1
        let right = getDeep(root.right)
        if(right==-1) return -1
        if(Math.abs(left-right)>1){
            return -1
        }else{
            return Math.max(left,right)+1
        }
    }
    return getDeep(root)===-1? false:true
};